package gui.client;

import java.awt.Color;
import java.awt.Image;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.Time;
import java.text.ParseException;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.text.MaskFormatter;

import system.FieldValidator;
import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import vcpClasses.Client;
import vcpClasses.Order;
import vcpClasses.SubscribedClient;

import javax.swing.JEditorPane;
import javax.swing.JTextField;

import control.LoginController;
import control.OrderController;
import control.SubscriptionController;
/**
 * this class use to Subscription Renewal Screen
 * @author G8
 *
 */
@SuppressWarnings("serial")
public class SubscriptionRenewalScreen extends JPanel implements ActionListener
{
	public JButton btnLogOut;
	public JButton btnBack;
	public JButton btnRenew;
	private JTextField txtSubNumber;
	private SubscriptionController subControl;
	/**
	 *Subscription Renewal Screen Constructor 
	 */
	public SubscriptionRenewalScreen()
	{
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
	private void initialize()
	{
		subControl = new SubscriptionController(null);
		
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Subscription Renewal Screen", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(SystemColor.menu);
		setBounds(VCP.guiX,VCP.guiY,500,400);
		setLayout(null);
		VCP.frame.setBounds(this.getBounds());
		
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(AdvanceOrderScreen.class.getResource("/img/back.png")));
		btnBack.setSize(150, 30);
		btnBack.setBounds(281, 22, 102, 35);
		btnBack.addActionListener(this);
		add(btnBack);
		
		btnLogOut = new JButton("Log Out");
		btnLogOut.setSize(150, 30);
		btnLogOut.setBounds(393, 22, 78, 35);
		btnLogOut.addActionListener(this);
		add(btnLogOut);
		
		btnRenew = new JButton("Renew");
		btnRenew.setBounds(183, 178, 120, 35);
		btnRenew.addActionListener(this);
		add(btnRenew);
		
		JLabel lblSubNumber = new JLabel("Subscription Number :");
		lblSubNumber.setBounds(108, 140, 160, 14);
		add(lblSubNumber);
		
		txtSubNumber = new JFormattedTextField();
		txtSubNumber.setBounds(278, 137, 90, 20);
		add(txtSubNumber);
	}
	
	
	
	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
	@Override
	public void actionPerformed(ActionEvent e)
	{

		if (e.getSource() == btnRenew)
		{
			txtSubNumber.setBackground(Color.WHITE);
			
			if (FieldValidator.checkNumeric(txtSubNumber.getText()))
			{
				SubscribedClient client = new SubscribedClient();
				client.setSubscriptionNumber(Integer.parseInt(txtSubNumber.getText()));
				client.setClientNumber(((Client)VCP.currentUser).getClientNumber());
				SystemTypes subToRenew = new SystemTypes(client, null);
				SystemTypes renewResult = subControl.renewSubscription(subToRenew);
				if (renewResult.getSysMode().equals(SystemMode.OK))
					{
						JOptionPane.showMessageDialog(null, renewResult.getObj().toString(), "Success",JOptionPane.INFORMATION_MESSAGE);
						btnRenew.setEnabled(false);
					}
				else
					JOptionPane.showMessageDialog(null,renewResult.getObj().toString(), "Error",JOptionPane.ERROR_MESSAGE);
			}
			else{
				txtSubNumber.setBackground(Color.RED);
				JOptionPane.showMessageDialog(null,"Invalid field(s) detected !", "Error",JOptionPane.ERROR_MESSAGE);
			}
		}
		
		if (e.getSource() == btnBack)
		{
			VCP.setScreen(this, VCP.prevScreen);
			VCP.prevScreen.setBounds(VCP.guiX,VCP.guiY,500,400);
			VCP.prevScreen.setLayout(null);
			VCP.frame.setBounds(VCP.prevScreen.getBounds());
		}
		
		if (e.getSource() == btnLogOut)
		{
			SystemTypes userToLogout = new SystemTypes(VCP.currentUser,null);
			boolean logoutRes = LoginController.LogOut(userToLogout);
			if (logoutRes)
				VCP.setGUI(0, this);
			else{
				JOptionPane.showMessageDialog(null, "Failed to logout!", "Error",
	                    JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
